package com.zxy.main;

import java.lang.reflect.Field;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.ParamName_InterFace;

@ServerEndpoint(value = "/websockets", configurator = WebSocketConfigurator.class)
public class WebSocketsStatic
{
	// 静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
	private static int onlineCount = 0;
	private Session session;
	
	@OnOpen
	public void onOpen(Session inputsession)
	{
		try
		{
			// inputsession.getBasicRemote().sendText("aaabbbccc");
	    	this.session = inputsession;
	        WebSocketUtils.serverClients.put(session.getId(), this);
			addOnlineCount();
		}
		catch(Exception e)
		{
		}
	}

	@OnClose
	public void onClose(Session inputsession, CloseReason closeReason)
	{
    	WebSocketUtils.serverClients.remove(inputsession.getId());
		subOnlineCount();
		// System.out.println("websocket client closed"
		// + closeReason.getReasonPhrase());
	}

	@OnError
	public void onError(Session inputsession, Throwable throwable)
	{
		// System.out.println("websocket error");
	}

	@OnMessage
	public void onMessage(String message, Session inputsession)
	{
		StringBuilder strResult = new StringBuilder(message);
		if(!Com_Para.WebSocket_IN_CLASS.equals(""))
		{
			try
			{
				Class<?> objC = Class.forName(Com_Para.WebSocket_IN_CLASS);
				ParamName_InterFace pni = (ParamName_InterFace) objC.newInstance();
				for(Field field: pni.getClass().getDeclaredFields())
				{
					field.setAccessible(true);
					if(field.getName().equals("strResult"))
						field.set(pni,strResult);
					else if(field.getName().equals("strIP"))
					{
						String strTemIP = (String) inputsession.getUserProperties().get("WebSocketClientIP");
						field.set(pni,strTemIP);
					}else if(field.getName().equals("inputsession"))
						field.set(pni,inputsession);
					// else if(field.getName().equals("bytesResult"))
					// field.set(pni,message.getPayload());
				}
				pni.init_start();
				for(Field field: pni.getClass().getDeclaredFields())
				{
					if(field.getName().equals("strResult"))
					{
						Object fieldGetName = Com_Fun.getFieldValueByName(field.getName(),pni);
						if(!fieldGetName.toString().equals(""))
						{
							inputsession.getBasicRemote().sendText(fieldGetName.toString());
							break;
						}
					}
				}
			}
			catch(Exception e)
			{
				Com_Fun.debug_in(e,this.toString(),Com_Para.WebSocket_IN_CLASS+"作为webSocket接收数据拦截器获取失败!\r\n"
						+ strResult.toString());
			}
		}
	}

	public static synchronized int getOnlineCount()
	{
		return onlineCount;
	}

	public static synchronized void addOnlineCount()
	{
		WebSocketsStatic.onlineCount++;
	}

	public static synchronized void subOnlineCount()
	{
		WebSocketsStatic.onlineCount--;
	}

	/**
	 * @return session
	 */
	public Session getSession()
	{
		return session;
	}

	/**
	 * @param session 要设置的 session
	 */
	public void setSession(Session session)
	{
		this.session = session;
	}
}